package com.right.health.app.ui.me;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AddressListResponse;
import com.right.health.app.bean.PayInfo;
import com.right.health.app.dialog.BaseDialog;
import com.right.health.app.dialog.ServiceCodeInputDialog;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.main.MainActivity;
import com.vendor.social.PayApi;
import com.vendor.social.model.PayType;
import com.vendor.social.pay.WxPay;

/**
 * @author: weming
 * @date: 2021/5/6
 * @description:服务包支付页
 */
public class ServiceOrderActivity extends BaseActivity implements OnHttpListener, PayApi.OnPayListener {

    private static final int HTTP_CODE_GET_CODE = 1;
    private static final int HTTP_CODE_GET_ADDRESS = 2;
    private static final int HTTP_CODE_PAY = 3;

    private static final String KEY_ID = "ID";
    private static final String KEY_NUM = "NUM";
    private static final String KEY_NAME = "NAME";
    private static final String KEY_HOSPITAL = "HOSPITAL";
    private static final String KEY_PRICE = "PRICE";

    private TextView mIdTv, mNameTv, mHospitalTv, mPriceTv, mAddressNameTV, mAddressTv;
    private RelativeLayout mWeChatPayLy;
    private Button mSureBtn;
    private UserApi mUserApi;
    private String mPhoneNum,mOrderId;
    private ProgressDialog mProgressDialog;
    private ServiceCodeInputDialog mInputDialog;
    private AddressListResponse.UserAddressBean mAddressBean;

    public static void launch(Activity activity, String id, String num,String name, String hospital, String price, int requestCode) {
        Intent intent = new Intent(activity, ServiceOrderActivity.class);
        intent.putExtra(KEY_ID, id);
        intent.putExtra(KEY_NUM, num);
        intent.putExtra(KEY_NAME, name);
        intent.putExtra(KEY_HOSPITAL, hospital);
        intent.putExtra(KEY_PRICE, price);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_service_order);
    }

    @Override
    public void findView() {
        mIdTv = findViewById(R.id.tv_order_num);
        mNameTv = findViewById(R.id.tv_name);
        mHospitalTv = findViewById(R.id.tv_hospital);
        mPriceTv = findViewById(R.id.tv_money);
        mWeChatPayLy = findViewById(R.id.rl_pay_wechat_item);
        mAddressNameTV = findViewById(R.id.tv_address_name);
        mAddressTv = findViewById(R.id.tv_address);
        mSureBtn = findViewById(R.id.btn_sure);
        mAddressTv.setOnClickListener(v -> {
            Intent intent = new Intent(this, ShippingAddressActivity.class);
            intent.putExtra("isSelect", true);
            startActivityForResult(intent, 1000);
        });
        mSureBtn.setOnClickListener(v -> {
            payReady();
        });
    }

    @Override
    public void initialize() {
        Intent intent = getIntent();
        mIdTv.setText(intent.getStringExtra(KEY_NUM));
        mNameTv.setText(intent.getStringExtra(KEY_NAME));
        mHospitalTv.setText(intent.getStringExtra(KEY_HOSPITAL));
        mPriceTv.setText(intent.getStringExtra(KEY_PRICE));
        mWeChatPayLy.setSelected(true);
        mPhoneNum = App.getInstance().getUser().mobilephone;
        mOrderId = intent.getStringExtra(KEY_ID);
        mUserApi = new UserApi();
        mUserApi.setListener(this);
        mUserApi.addRequestCode(HTTP_CODE_GET_ADDRESS);
        mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
        mUserApi.getDefaultAddress();
    }

    private void payReady() {
        if (mAddressBean == null) {
            ToastUtil.show(this, "地址未选择！");
        } else {
            if (mInputDialog == null) {
                mInputDialog = new ServiceCodeInputDialog(this, mPhoneNum, v -> {
                    mProgressDialog = ProgressDialog.show(ServiceOrderActivity.this, "", "加载中", false, true);
                    mUserApi.addRequestCode(HTTP_CODE_GET_CODE);
                    mUserApi.getServiceCode(mPhoneNum);
                });
                mInputDialog.setDelegate(dialog -> {
                    String code = mInputDialog.getInput();
                    if (TextUtils.isEmpty(code)) {
                        ToastUtil.show(this, "请输入验证码");
                    } else {
                        dialog.dismiss();
                        mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
                       mUserApi.addRequestCode(HTTP_CODE_PAY);
                        mUserApi.payServicePack(mOrderId,"10",mAddressBean.getAddressid(),mPhoneNum,code);
                    }
                });
                mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
                mUserApi.addRequestCode(HTTP_CODE_GET_CODE);
                mUserApi.getServiceCode(mPhoneNum);
            }else {
                mInputDialog.show();
            }
        }
    }


    @Override
    public void onResponse(Request request, Response response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_GET_ADDRESS) {
                try {
                    AddressListResponse listResponse = response.cast(AddressListResponse.class);
                    if (!CollectionUtil.isEmpty(listResponse.getUserAddress())) {
                        mAddressBean = listResponse.getUserAddress().get(0);
                        mAddressNameTV.setText(mAddressBean.getUsername() + "  " + mAddressBean.getUserphone());
                        mAddressTv.setText(mAddressBean.getUseraddress());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            } else if (request.requestCode == HTTP_CODE_GET_CODE) {
                ToastUtil.show(this, "验证码已发送");
                mInputDialog.startTiming();
                if (!mInputDialog.isShowing()) {
                    mInputDialog.show();
                }
            } else if (request.requestCode == HTTP_CODE_PAY) {
                if(response.targetData instanceof PayInfo) {
                    PayInfo payInfo = response.cast(PayInfo.class);
                    if(!TextUtils.isEmpty(payInfo.orderstr)) {
                        PayApi.pay(this, PayType.ALIPAY, payInfo.orderstr, this);
                    } else if (payInfo.wxdata!=null) {
                        WxPay wxPay = new WxPay(this);
                        wxPay.setOnPayListener(this);
                        wxPay.pay(payInfo.wxdata);
                    }

                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (data != null) {
                try {
                    mAddressBean = (AddressListResponse.UserAddressBean) data.getSerializableExtra("result");
                    if (mAddressBean != null) {
                        mAddressNameTV.setText(mAddressBean.getUsername() + "  " + mAddressBean.getUserphone());
                        mAddressTv.setText(mAddressBean.getUseraddress());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onPayOk() {
        ToastUtil.show(this, "支付成功");
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onPayFail(String msg) {
        ToastUtil.show(this, msg);
    }
}
